﻿using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using AirIndex.Models.HtmlClient.SendData;
using HVACWebService.Business;
using HVACWebService.Cloud.Able;
using HVACWebService.Common;
using HVACWebService.DomainModel.Able;

namespace HVACWebService.Controllers
{
    public class HavcDataController :ApiController
    {

        [HttpGet]
        public string GetAirQuality(string openId, string did, string pid)
        {

            //获取设备实时数据
            var deviceData = "";
            HttpContent content = Request.Content;
            Task readTask = content.ReadAsStringAsync().ContinueWith((task) =>
            {
                try
                {
                    GetDeviceData(openId, did, pid, returnData =>
                    {
                        deviceData = returnData;
                    });
                }
                catch (Exception ex)
                {


                }

            });
            readTask.Wait();
            return deviceData;
        }

        private void GetDeviceData(string openId, string deviceId, string physicalDeviceId, Action<string> ableDeviceDataAction)
        {
            var ableDeviceCacheService = new AbleDeviceCloudService();

            var returnData = ableDeviceCacheService.GetUserDeviceInfo(openId, deviceId, physicalDeviceId);
            ableDeviceDataAction.Invoke(returnData);
        }



        [HttpGet]
        public Tokens GetAccessToken(string key)
        {
            //获取 js 在线 token
            var pageUrl = "";
            if (key == "htmlpage")
            {
                pageUrl = ConfigHelper.GetValue<string>("HtmlService5Url") + ConfigHelper.GetValue<string>("HtmlPagePath");

            }
            var weiXinTokenCloudService = new WeChatTokenService();
            var acc = weiXinTokenCloudService.GetJsApiTokens(pageUrl);
            acc.appSecret = "";
            return acc;


        }

        [HttpGet]
        public AbleShareCode GetShareCode(string deviceId, string physicalDeviceId, string openId)
        {
            //获取 分享码
            var ableDeviceCloudService = new ShareDeviceService();
            return ableDeviceCloudService.GetShareCode(deviceId, physicalDeviceId, openId);
        }

        [HttpGet]
        public int ShareCodeBindDevice(string sourceOpenId, string deviceId, string ableDeviceId, string shareOpenId, string shareCode)
        {
            shareCode = shareCode.Replace("@", "#");
            //绑定分享设备
            var ableDeviceCloudService = new ShareDeviceService();
            return ableDeviceCloudService.ShareCodeBindDevice(sourceOpenId, deviceId, ableDeviceId, shareOpenId, shareCode);

        }

        [HttpGet]
        public int BindingDevice(string openId, string pId, string deviceName,
            string phoneNumber, string address)
        {
            //绑定设备
            var customerDeviceService = new CustomerDeviceService();
            return customerDeviceService.BindingDevice(openId, pId, deviceName, phoneNumber, address);
        }


        [HttpGet]
        public bool UnBindingDevice(string openId, string pId)
        {
            //绑定设备
            var customerDeviceService = new CustomerDeviceService();
            return customerDeviceService.UnbindDevice(openId, pId);
        }


        [HttpGet]
        public AbleDevicesExtend GetUserDevices(string openId)
        {
            //获取设备列表
            var ableDeviceCloudService = new DeviceInfoService();
            return ableDeviceCloudService.GetUserDevices(openId);
        }

        [HttpGet]
        public byte[] SendToDevice(string openId, string sendData, string physicalDeviceId, string deviceId,
        string mesageCode)
        {
            //发送设备控制指令
            var ableDevice = new SendToDeivceService();
            return ableDevice.SendToDevice(openId, sendData, physicalDeviceId, deviceId, mesageCode);

        }
    }
}